/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Attributes.LibraryMetadataExtract;
import Records.RecordsMain;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author Alexander
 */
public class ImportSongs {

    static void walkTree(File topFile, List<File> fileList) {
        System.out.println("ImportSongs.walkTree()");

        if (topFile.isDirectory()) {
            for (File f : topFile.listFiles()) {
                walkTree(f, fileList);
            }
        } else if (topFile.isFile()) {
            if (FilenameUtils.isExtension(topFile.getAbsolutePath(), "m4a") || FilenameUtils.isExtension(topFile.getAbsolutePath(), "mp3")) {
                fileList.add(topFile);
                RecordsMain.addToFilePathList(topFile.getAbsolutePath());
            }
        }
    }

    public static void importSongs(String Dir) {
                System.out.println("ImportSongs.importSongs(" + Dir +")");

        List<File> fileList = new LinkedList<>();
        walkTree(new File(Dir), fileList);
        LibraryMetadataExtract meta;
        for (File f : fileList) {
            meta = new LibraryMetadataExtract(f);
            meta.extract();
        }

        DefaultTableModel model = (DefaultTableModel) RecordsMain.getUI().userLibrary.getModel();

        System.out.println("all songs have been imported");
        UpdateLibraryImp.updater();
    }
}
